package com.zls.service.impl;

import com.zls.dto.ContractorDto;
import com.zls.dto.DeveloperDto;
import com.zls.mapper.ContractorMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.Contractor;
import com.zls.pojo.Project;
import com.zls.service.ContractorService;
import com.zls.service.ProjectService;
import com.zls.utils.UUIDUtils;
import com.zls.vo.ConProVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ContractorServiceImpl implements ContractorService {
    @Autowired
    private ContractorMapper contractorMapper;
    @Autowired
    private ProjectService projectService;

    @Override
    public List<Contractor> findAll() {
        return contractorMapper.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<Contractor> page = contractorMapper.findCon((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = contractorMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional
    //添加承建商，将工程进行绑定
    public int addAndBind(String cname, int[] pids) {
        Contractor contractor=new Contractor();
        contractor.setCid(UUIDUtils.uuid());
        contractor.setCname(cname);
        contractor.setUpdatetime(new Date());
        int add = contractorMapper.add(contractor);
        //绑定工程
        projectService.bindCid(contractor.getCid(),pids);
        return add;
    }

    //删除逻辑：删除开发商的时候，同时要将工程对应的开发商的id设置为null
    @Override
    @Transactional
    public int deleteCon(String cid) {
        projectService.deleteByCid(cid);
        int i = contractorMapper.deleteCon(cid);
        return i;
    }

    //cid查找修改
    @Override
    public ContractorDto findByCid(String cid) {
        ContractorDto contractorDto = contractorMapper.findByCidDto(cid);
        List<Project> list = projectService.findByCids(cid);
        int[] pids=null;
        if (list!=null){
            pids=new int[list.size()];
            for (int i = 0; i <list.size() ; i++) {
                pids[i]=list.get(i).getPid();
            }
            contractorDto.setPids(pids);
        }
        return contractorDto;
    }

    //修改
    @Transactional
    @Override
    public int editCon(ConProVo conProVo) {
        conProVo.setUpdatetime(new Date());
        int i = contractorMapper.updateCon(conProVo);
        //同时更新project的cid
        projectService.deleteByCid(conProVo.getCid());
        //绑定工程
        projectService.bindCid(conProVo.getCid(),conProVo.getPids());
        return i;
    }
}